scrollToElementById = function (id) {
    location.href = `${location.protocol}//${location.hostname}${location.port ? ':' + location.port : ''}${location.pathname}#${id}`
}

/**
 * xml 代码不能心文本形式显示在页面上，需要转换一下
 * @param selector
 */
initXmlPreTagInner = function (selector) {
    const preList = $(selector)
    if (preList && preList.length) {
        for (let i = 0; i < preList.length; i++) {
            const preDom = preList[i];
            const inner = preDom.innerHTML || '';
            preDom.innerHTML = null
            preDom.appendChild(document.createTextNode(inner))
        }
    }
};

const srcDialogTemplate = `
<div class="modal fade" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-scrollable modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5">Modal title</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
            <pre class="small-light-height-pre"></pre>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>`

function initSrcEvent() {
    const buttons = $("button[show-src]")
    if (!buttons || !buttons.length) {
        return
    }
    for (let i = 0; i < buttons.length; i++) {
        const button = $(buttons[i])
        button.on('click', showSrcButtonEventHandler(button.attr('file-src'), button.attr('src-name')))
    }
}

function showSrcButtonEventHandler(src, title) {
    if (!window.globalDialog) {
        window.globalDialog = {}
        if (!window.globalDialog.srcDialog) {

            window.globalDialog.srcDialog = $(srcDialogTemplate)[0]
            window.globalDialog.srcDialog.modalInstance = new bootstrap.Modal(window.globalDialog.srcDialog)
            document.body.appendChild(window.globalDialog.srcDialog)
        }
    }
    const srcDialog = window.globalDialog.srcDialog
    // 给body 设置内容
    const body = srcDialog.querySelector(".modal-body pre")
    const titleDom = srcDialog.querySelector(".modal-title")
    return function () {
        titleDom.innerHTML = title
        body.innerHTML = '正在加载...'
        srcDialog.modalInstance.show()
        $.ajax(src, {
            type: 'get',
            success: function (text, status, response) {
                body.innerHTML = ''
                if (typeof text === 'string') {
                    body.appendChild(document.createTextNode(text));
                } else if (text instanceof XMLDocument) {
                    body.appendChild(document.createTextNode(response.responseText))
                } else {
                    body.innerHTML = '不支持的文档类型：' + src
                }
            },
            error: function () {
                body.innerHTML = `获取资源：${src} 出错。`
                console.error(arguments)
            },
        })
    };
}

/**
 * 异步加载代码
 */
function lazyLoadSrc() {
    const lazyPre = $("div[lazy-src]");
    if (!lazyPre || !lazyPre.length) {
        return
    }

    for (const lazyPreElement of lazyPre) {
        const src = lazyPreElement.attributes.getNamedItem('src');
        const srcStr = src ? src.value : null;
        if (!srcStr) {
            continue
        }
        $.ajax(srcStr, {
            type: 'get',
            success: function (text, status, response) {
                const pre = document.createElement('pre')
                lazyPreElement.appendChild(pre)
                pre.appendChild(document.createTextNode(response.responseText))
                if (typeof addMaxEvent === 'function') {
                    addMaxEvent(lazyPreElement)
                }
            },
            error: function () {
                lazyPreElement.innerHTML = "加载资源出错：" + srcStr;
                console.error(arguments)
            },
        })
    }


}

/**
 * 警告条
 */
let alertPlaceholder
function bsAlert(message, type) {
    if (!alertPlaceholder) {
        alertPlaceholder = document.createElement('div')
        alertPlaceholder.classList.add('alert-holder')
        document.body.appendChild(alertPlaceholder)
    }


    const wrapper = document.createElement('div');
    if (typeof message === 'string') {
        wrapper.innerHTML = [
            `<div class="alert alert-${type} alert-dismissible" role="alert">`,
            `   <div>${message}</div>`,
            '   <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
            '</div>'
        ].join('')
    } else {
        wrapper.appendChild(message)
        message.style.display = 'block';
    }

    alertPlaceholder.append(wrapper)
    wrapper.addEventListener('close.bs.alert', () => {
        setTimeout(() => {
            alertPlaceholder.removeChild(wrapper)
        }, 500)
    })
}
